<?php
    Class Group_attributes extends CI_Controller{
        var $data = array();

        function __construct()
        {
            parent::__construct();
            
            if(!$this->session->userdata('logged_in'))
                redirect(base_url('login'));
            
            $this->load->model('group_attributes_model');
            $this->load->model('groups_model');
            $this->load->model('objects_model');
            $this->load->helper('inflector');
            $this->load->helper('component');
            
            $this->load->config('pages');
            $this->load->library('translate', array('lang' => 'en'));
        }

        /**
        * get_attributes_on_group
        * 
        * List all attributes for provided group
        * 
        * @param $group_id
        * 
        */
        public function get_attributes_on_group($group_id)
        {
            // get attributes on provided group
            $data['group_attributes'] = $this->group_attributes_model->get_attributes_on_group($group_id);
            $data['group_id'] = $group_id;
            $html = $this->load->view('group_attributes/view_attributes_on_group',$data,TRUE);
            echo $html;
        }

        /**
        * get_custom_attributes_on_group
        * 
        * List all custom attributes for provided group
        * 
        * @param $group_id
        * 
        */
        public function view_add_attribute_on_group($group_id)
        {
            // get custom attributes on provided group
            $data['group_attributes'] = $this->group_attributes_model->get_attributes_on_group($group_id,TRUE,0);
            $data['group_id'] = $group_id;
            $html = $this->load->view('group_attributes/add_attribute_on_group',$data,TRUE);
            echo $html;
        }

        /**
        * submit_add_attribute_on_group
        * 
        * Add inactive group_attribute to group
        * 
        * @param $_POST
        * 
        */
        public function submit_add_attribute_on_group()
        {
            $validation_info = '<b>attribute</b>';

            $this->form_validation->set_rules("attribute_id", " ", "dropdown_check[$validation_info]");
            if ($this->form_validation->run() == FALSE)
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
                return;
            }

            $this->group_attributes_model->set_active_group_attribute($_POST['group_id'],$_POST['attribute_id'],1);
            echo json_encode(array('action'=>TRUE,'msg'=>'Attribute added to group')); 
        }

        /**
        * reorder_group_attributes
        * 
        * Set sort column on group_attrinutes
        * 
        * @param $_POST
        * 
        */
        public function reorder_group_attributes()
        {
            $group_id = $_POST['group_id'];
            unset($_POST['group_id']);

            $cnt = 0;
            foreach ($_POST as $value):
                $id = explode('_',$value);
                $this->db->where('groups_ID',$group_id);
                $this->db->where('attributes_ID',$id[1]);
                $this->db->update('group_attributes',array('sort'=>$cnt));
                $cnt++;
                endforeach;
        }

        /**
        * get_html_on_group
        * 
        * Get html element on provided group
        * 
        * @param $group_id
        * @param $mode
        * @param $object_id
        * 
        */
        public function get_html_on_group($group_id,$mode,$object_id=NULL)
        {

            // get attributes on provided group
            $group_attributes = $this->group_attributes_model->get_attributes_on_group($group_id);

            switch ($mode) {
                case "EDIT":

                    // get object
                    $object = $this->objects_model->list_by_id($object_id);
                    // get HTML on provided group
                    $group_html = $this->groups_model->get_html_on_group($group_attributes,$object['object_fields'],$group_id); 

                    break;
                case "NEW":

                    // get HTML on provided group
                    $group_html = $this->groups_model->get_html_on_group($group_attributes,NULL,$group_id); 

                    break;
            }

            echo json_encode(array('action'=>TRUE,'group_html'=>$group_html));
        }

        /**
        * set_active_group_attribute
        * 
        * Set group_attribute visible on form
        * 
        * @param group_id
        * @param attribute_id
        * 
        */
        public function set_active_group_attribute($group_id,$attribute_id)
        {
            $this->group_attributes_model->set_active_group_attribute($group_id,$attribute_id,0);
            echo json_encode(array('action'=>TRUE)); 
        }
    }

?>
